<template>
  <div>
    <Delagency ref="del"></Delagency>
    <a-card class="general-card">
      <a-table
        row-key="id"
        :loading="loading"
        :data="renderData"
        :bordered="false"
        :pagination="false"
      >
        <template #columns>
          <a-table-column :title="props.position" data-index="title" />
          <a-table-column
            :title="$t('columns.operations')"
            align="center"
            data-index="operations"
            :width="250"
          >
            <template #cell="{ record }">
              <a-button
                type="text"
                size="small"
                @click="handleClickToSave(record, 'DEL')"
              >
                废除
              </a-button>
              <a-button
                type="text"
                size="small"
                @click="handleClickToSave(record, 'REV')"
              >
                撤回
              </a-button>
              <a-button
                type="text"
                size="small"
                @click="handleClickToSave(record, 'DETAIL')"
              >
                详情
              </a-button>
            </template>
          </a-table-column>
        </template>
      </a-table>
    </a-card>
  </div>
</template>

<script lang="ts" setup>
  import { ref } from 'vue';
  import { useUserStore } from '@/store';
  import useLoading from '@/hooks/loading';
  import { TableData, Message } from '@arco-design/web-vue';
  import {
    acquireMyApply,
    QueryMyApplyDTO,
    TodoVO,
    sendTodoDone,
    queryNode,
    getCurrentTaskListByFormId,
    withdrawalofApplication,
    HandleFlowDTO,
  } from '@/api/agency';
  import Delagency from './delagency.vue';

  const props = defineProps({
    position: {
      type: String,
      default: 'agency',
    },
  });
  const UserStore = useUserStore();
  const { loading, setLoading } = useLoading();
  const renderData = ref<TableData[]>([]);
  const positionFormModel = () => {
    return {
      dealer: UserStore.user.id || '', // 审批人编码
      formId: null, // 表单id 可模糊查询
      status: null, // 状态 NORMAL审批中,DRAFT草稿,DELETED作废,OVER结束
      title: null, // 标题 可模糊查询
    };
  };
  const formModel = ref(positionFormModel());
  const sendFormModel = () => {
    return {
      applicationCode: null, // 应用code
      realDealer: UserStore.user.id || null, // 真实处理人
      realDealerName: UserStore.user.name || null, // 真实处理人名称
      taskId: '', // 任务id
    };
  };
  const parmas = ref(sendFormModel());
  const fetchData = async (
    params: QueryMyApplyDTO = { ...formModel.value }
  ) => {
    setLoading(true);
    try {
      const { data } = await acquireMyApply(params);
      renderData.value = data;
    } catch (err) {
      // you can report use errorHandler or other
    } finally {
      setLoading(false);
    }
  };
  const del = ref();
  const withdrawalofApplicationData = () => {
    return {
      assignees: [] as string[],
      backActivityId: '', // 驳回至节点ID
      cascade: null, //  是否级联
      formId: '', //  表单ID
      multiSignTag: null, // 会签处理人变量名
      opinion: 'REVOKE', //  处理意见
      parameters: {},
      processInstanceId: null, //  流程实例id
      remark: null, //  说明
      taskId: '', //  任务Id
      toUserId: null, //  转办给用户ID
      toUserName: null, // 转办给用户姓名
    };
  };
  const withdrawalParmas = ref(withdrawalofApplicationData());
  const handleClickToSave = (obj: TodoVO, type: string) => {
    if (type === 'DEL') {
      del.value.handleChangeVisible(obj.formId);
    } else if (type === 'SEND') {
      if (obj.taskId) parmas.value.taskId = obj.taskId;
      sendTodoDone(parmas.value).then(() => {
        Message.success('操作成功');
        fetchData();
      });
    } else if (type === 'REV') {
      queryNode(obj.formId).then((res) => {
        res.data.forEach((item: any) => {
          // withdrawalParmas.value.assignees.push(item.assignee);
          withdrawalParmas.value.backActivityId =
            item.activityName === '起草'
              ? item.activityId
              : withdrawalParmas.value.backActivityId;
        });
        withdrawalParmas.value.formId = obj.formId;
        getCurrentTaskListByFormId(obj.formId).then((data) => {
          withdrawalParmas.value.taskId = data.data[0].taskVO.id;
          withdrawalofApplication(withdrawalParmas.value).then((da) => {
            Message.success('操作成功');
            fetchData();
          });
        });
      });
    } else if (type === 'DETAIL') {
      const url = ref(obj.path.includes('http'));
      if (url.value === true) {
        window.open(
          `${obj.path}&taskId=${obj.taskId ? obj.taskId : ''}`,
          '_blank'
        );
      } else {
        window.open(
          `${window.location.hostname}/${obj.path}?taskId=${
            obj.taskId ? obj.taskId : ''
          }`,
          '_blank'
        );
      }
    }
  };
  fetchData();
</script>

<style scoped lang="less">
  :deep(.arco-table-th) {
    &:last-child {
      .arco-table-th-item-title {
        margin-left: 16px;
      }
    }
  }
</style>
